package com.qf.team2.netdisc.controller;

import com.qf.team2.netdisc.common.base.BaseController;
import com.qf.team2.netdisc.common.pojo.entity.Document;
import com.qf.team2.netdisc.common.pojo.vo.RespResultVO;
import com.qf.team2.netdisc.common.util.FileUtil;
import com.qf.team2.netdisc.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author
 */
@Controller
@RequestMapping("document")
public class DocumentDownloadController extends BaseController<Object> {

    @Autowired
    private DocumentService documentService;

    @ResponseBody
    @RequestMapping(value = "download", method = RequestMethod.POST)
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String documentid = request.getParameter("documentId");

        String[] split = documentid.split(",");

        if (split.length > 1) {
            Integer documentId1 = Integer.parseInt(split[0]);
            Document document1 = documentService.getDocumentById(documentId1);

            String documentUrl1 = document1.getUrl();
            String[] split1 = documentUrl1.split("\\\\");

            String url = split1[split1.length - 2];

            response.reset();
            response.setContentType("application/x-zip-compressed;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;documentname=" + url + ".zip");

            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
            BufferedOutputStream bos = new BufferedOutputStream(zos);

            for (int i = 0; i < split.length; i++) {

                Integer documentId = Integer.parseInt(split[i]);

                Document document = documentService.getDocumentById(documentId);
                String documentName = document.getName();
                String documentUrl = document.getUrl();

                System.out.println("documentName=" + documentName);
                System.out.println("documentUrl=" + documentUrl);

                //这个zip文件的字节
                FileInputStream documentInputStream = new FileInputStream(documentUrl);

                zos.putNextEntry(new ZipEntry(documentName));

                int len = 0;
                byte[] buf = new byte[10 * 1024];
                while ((len = documentInputStream.read(buf, 0, buf.length)) != -1) {
                    bos.write(buf, 0, len);
                }
                documentInputStream.close();
                bos.flush();
            }
            bos.close();
        } else {
            Integer documentId = Integer.parseInt(split[0]);

            Document document = documentService.getDocumentById(documentId);
            String documentName = document.getName();
            String documentUrl = document.getUrl();

            FileUtil.downloadFile(response, documentName, documentUrl);

        }

    }

    @ResponseBody
    @RequestMapping(value = "checkDocument", method = RequestMethod.POST)
    public RespResultVO<Object> checkDocument(String url) {
        System.out.println("url=" + url);
        try {
            String data = documentService.checkDocument(url);

            return successResp(data, "有验证码");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return failedResp(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "checkCode",produces = "application/json",method = RequestMethod.POST)
    public RespResultVO<Object> checkCode(String url, String code) {
        String link = documentService.checkCode(url, code);

        return successResp(link, "验证验证码");

    }

}
